/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.draw;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GCubicCurve;
import br.ufmg.dees.insane.model.geo.GCurve;
import br.ufmg.dees.insane.model.geo.GLine;
import br.ufmg.dees.insane.model.geo.GPoint;
import br.ufmg.dees.insane.model.geo.GQuadCurve;
import br.ufmg.dees.insane.model.geo.GRegion;

public class RegionDraw extends Draw {
	
	DrawingArea da;
	int mediumX = 0;
	int mediumY = 0;
	
//*****************************************************************************
	
	public RegionDraw(DrawingArea da, Object represents) {
		Point p1 = da.getViewPoint( ((GRegion)represents).getFirstPoint() );
		this.setLocation(p1);
		this.da = da;
		this.setRepresents(represents);
	}
	
//*****************************************************************************
	
	public boolean equals(Object obj) {
/*		if (super.equals(obj) && obj instanceof RegionDraw) {
			RegionDraw d = (RegionDraw)obj;
			if (d.getP2().equals(getP2())){
				return true;
			}
		}
*/		return false;
	}
	
//*****************************************************************************
	
	public Point getP1() {
		return this.getLocation();
	}
	
//*****************************************************************************
	
	public Point getP2() {
		Point po = new Point(this.getSize().width, this.getSize().height);
		return po;
	}
	
//*****************************************************************************
	
	public double getLength() {
		double dx = this.getX2() - this.getX1();
		double dy = this.getY2() - this.getY1();
		double l = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
		return l;
	}
	
//*****************************************************************************
	
    public double getX1() {
        return getLocation().x;
    }
	
//*****************************************************************************
	
    public double getY1() {
        return getLocation().y;
    }
	
//*****************************************************************************
	
    public double getX2() {
        return getSize().width;
    }
	
//*****************************************************************************
	
    public double getY2() {
        return getSize().height;
    }
	
//*****************************************************************************
	
    public Vector<Object> getShapes() {
        return null;
    }
	
//*****************************************************************************
	
	public double getAngle() {
		double theta;
		double dx = getX2() - getX1();
		double dy = getY2() - getY1();
		theta = Math.atan2(-dy, dx);
		return theta;
	}
	
//*****************************************************************************
	
    /**
     * Draws in a given Graphics.
     *
     * @see   java.awt.Graphics
     */
	public void draw(Graphics2D g2) {
		GRegion rg = (GRegion)getRepresents();
		GeneralPath region = getRegion();
		g2.fill(region);
		
		Color currentColor = g2.getColor();
		if (isSelected()) {
			g2.setColor( Color.red );
			g2.fill(region);
			if ( rg.getElements().isEmpty() ) {
				g2.setColor( Color.black );
				this.drawLabel(g2);
			}
		}
		g2.setColor(currentColor);
	}
	
//*****************************************************************************
	
	public void drawLabel(Graphics2D g2) {
		GRegion rg = (GRegion)getRepresents();
		GeneralPath region = getRegion();
		if ( region.contains(mediumX,mediumY) ) {
			g2.drawString( "R" + rg.getLabel(), mediumX, mediumY );
		}
	}
	
//*****************************************************************************
	
	public void drawBoundary(Graphics2D g2) {
		GeneralPath region = getRegion();
		g2.draw(region);
	}
	
//*****************************************************************************
	
	/**
	 * Returns the draw bounds in device units.
	 *
	 * @return the draw bounds
	 */
	public Rectangle getBounds() {
		GeneralPath region = getRegion();
		return region.getBounds();
	}
    
//*****************************************************************************
	
	public boolean contains(Point p) {
		GeneralPath region = getRegion();
		return region.contains(p);
	}
	
//*****************************************************************************
	
	public GeneralPath getRegion() {
		GRegion rg = (GRegion)getRepresents();
		GeneralPath region = new GeneralPath();
		GPoint first = rg.getFirstPoint();
		Point p1 = da.getViewPoint(first);
		Point p2, p3;
		float x4 = (float)p1.getX();
		float y4 = (float)p1.getY();
		region.moveTo(x4,y4);
		List curves = rg.getCurves();
		int x = 0;
		int y = 0;
		int np = 0;
		int cv = 0;
		ListIterator cvs = curves.listIterator();
       	while(cvs.hasNext()) {
			cv++;
			GCurve gcv = (GCurve)cvs.next();
			p1 = da.getViewPoint(gcv.getFirstPoint());
			x4 = (float)p1.getX();
			y4 = (float)p1.getY();
			region.lineTo(x4,y4);
			x = x + (int)x4;
			y = y + (int)y4;
			np++;
			List primitives = gcv.getPrimitives();
			ListIterator prts = primitives.listIterator();
			while(prts.hasNext()) {
				Object obj = prts.next();
				if (obj instanceof GPoint) {
					GPoint prt = (GPoint)obj;
					p1 = da.getViewPoint(prt.getP2());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					region.lineTo(x1,y1);
					x = x + (int)x1;
					y = y + (int)y1;
					np++;
				}
				if (obj instanceof GLine) {
					Line2D prt = (Line2D)obj;
					p1 = da.getViewPoint(prt.getP2());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					region.lineTo(x1,y1);
					x = x + (int)x1;
					y = y + (int)y1;
					np++;
				}
				if (obj instanceof GQuadCurve) {
					GQuadCurve prt = (GQuadCurve)obj;
					p1 = da.getViewPoint(prt.getCtrl());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					p2 = da.getViewPoint(prt.getP2());
					float x2 = (float)p2.getX();
					float y2 = (float)p2.getY();
					region.quadTo(x1,y1,x2,y2);
					x = x + (int)x2;
					y = y + (int)y2;
					np++;
				}
				if (obj instanceof GCubicCurve) {
					GCubicCurve prt = (GCubicCurve)obj;
					p1 = da.getViewPoint(prt.getCtrl1());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					p2 = da.getViewPoint(prt.getCtrl2());
					float x2 = (float)p2.getX();
					float y2 = (float)p2.getY();
					p3 = da.getViewPoint(prt.getP2());
					float x3 = (float)p3.getX();
					float y3 = (float)p3.getY();
					region.curveTo(x1,y1,x2,y2,x3,y3);
					x = x + (int)x3;
					y = y + (int)y3;
					np++;
				}
			}
		}
		region.closePath();
		mediumX = (int)(x/np) - 5;
		mediumY = (int)(y/np) + 5;
		return region;
	}

//*****************************************************************************
}
